package com.lv.phuotbacnam.ui.fragment;




import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import com.lv.phuotbacnam.R;
import com.lv.phuotbacnam.bl.queries.QueriesConstant;
import com.lv.phuotbacnam.bl.queries.QueriesConstant.TableUser;
import com.lv.phuotbacnam.bl.utils.IntentHelper;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.parse.ParseUser;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;


public class edit_ten extends Activity  {
	public ImageLoader imageLoader;
	public String tendaydu = "";
	public String mail = "";
	public String diachi = "";
	public String dienthoai;
	public String ngaysinh;
	public String gioitinh = "";
	public String diemhienthi = "";
	public ImageLoader image;
	ParseUser user = ParseUser.getCurrentUser();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);		
		setContentView(R.layout.edit_ten);
		
		// get intent from thongtincanhan
		
		Intent i = getIntent();
		tendaydu = i.getStringExtra("tendaydu");
		diachi = i.getStringExtra("diachi");
		ngaysinh = i.getStringExtra("ngaysinh");
		gioitinh = i.getStringExtra("gioitinh");
		dienthoai = i.getStringExtra("dienthoai");
		
		// 
		imageLoader = ImageLoader.getInstance();
		ImageView profileImage = (ImageView) findViewById(R.id.left_menu_pic);
		imageLoader.displayImage(user.getString(TableUser.COLUMN_photoURL),
				profileImage);

		TextView profileFullName = (TextView) findViewById(R.id.left_menu_fullname);
		profileFullName.setText(tendaydu);
		
		image = ImageLoader.getInstance();
		final ImageView im = (ImageView) findViewById(R.id.anhbia);
		image.displayImage(user.getString(TableUser.COLUMN_anhbia),
				im);
		
		final EditText  edt_tendaydu = (EditText) findViewById(R.id.tendaydu);
		final EditText  edt_diachi = (EditText) findViewById(R.id.diachi);
		final EditText  edt_ngaysinh = (EditText) findViewById(R.id.ngaysinh);
		final EditText  edt_dienthoai = (EditText) findViewById(R.id.dienthoai);
		final RadioButton rad_nam = (RadioButton) findViewById(R.id.radio_nam);
		final RadioButton rad_nu = (RadioButton) findViewById(R.id.radio_nu);
		Button btn_save = (Button) findViewById(R.id.btn_save);
		
		edt_tendaydu.setText(tendaydu);
		edt_diachi.setText(diachi);
		edt_dienthoai.setText(dienthoai);
		edt_ngaysinh.setText(ngaysinh+"");
		
		if (gioitinh != "Nam") rad_nam.setChecked(true);
		else rad_nu.setChecked(true);
		/*
		 * event click profileImage
		 */
		profileImage.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				ParseUser user = ParseUser.getCurrentUser();
				Intent intent = new Intent(edit_ten.this,
						thongtincanhan.class);
				IntentHelper.addObjectForKey(user, "user");
				startActivity(intent);
				finish();
			}
		});
		/*
		 * event click button save 
		 */
		btn_save.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				user.put(QueriesConstant.TableUser.COLUMN_firstName, edt_tendaydu.getText().toString());
				user.put(QueriesConstant.TableUser.COLUMN_lastName, "");
				user.put(QueriesConstant.TableUser.COLUMN_dia_chi, edt_diachi.getText().toString());
				user.put(QueriesConstant.TableUser.COLUMN_phone, edt_dienthoai.getText().toString());
				if (rad_nam.isChecked()) 
					user.put(QueriesConstant.TableUser.COLUMN_gioitinh, "Nam".toString());
				else 
					user.put(QueriesConstant.TableUser.COLUMN_gioitinh, "Nữ".toString());
				user.put(QueriesConstant.TableUser.COLUMN_ngaysinh, edt_ngaysinh.getText().toString());
				user.saveInBackground();
				ParseUser user = ParseUser.getCurrentUser();
				Intent intent = new Intent(edit_ten.this,
						thongtincanhan.class);
				IntentHelper.addObjectForKey(user, "user");
				startActivity(intent);				
				finish();
				
			}
		});
	}	
	public void onRadioButtonClicked(View view) {
	    // Is the button now checked?
	    boolean checked = ((RadioButton) view).isChecked();
	    
	    // Check which radio button was clicked
	    switch(view.getId()) {
	        case R.id.radio_nam:
	            if (checked)
	                gioitinh = "Nam";
	            break;
	        case R.id.radio_nu:
	            if (checked)
	                gioitinh = "Nữ";
	            break;
	    }
	}
}
